package com.ruoyi.system.controller;

import java.util.List;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YwYh;
import com.ruoyi.system.service.IYwYhService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-21
 */
@Controller
@RequestMapping("/system/yh")
public class YwYhController extends BaseController
{
    private String prefix = "system/yh";

    @Autowired
    private IYwYhService ywYhService;

    @RequiresPermissions("system:yh:view")
    @GetMapping()
    public String yh()
    {
        return prefix + "/yh";
    }

    /**
     * 查询用户管理列表
     */
    @RequiresPermissions("system:yh:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YwYh ywYh)
    {
        startPage();
        List<YwYh> list = ywYhService.selectYwYhList(ywYh);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @RequiresPermissions("system:yh:export")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YwYh ywYh)
    {
        List<YwYh> list = ywYhService.selectYwYhList(ywYh);
        ExcelUtil<YwYh> util = new ExcelUtil<YwYh>(YwYh.class);
        return util.exportExcel(list, "用户管理数据");
    }
    /**
     * 用户导入
     */
    @RequiresPermissions("system:yh:import")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YwYh> util = new ExcelUtil<YwYh>(YwYh.class);
        List<YwYh> userList = util.importExcel(file.getInputStream());
        String message = ywYhService.importYwYh(userList, updateSupport,"yidian");
        return AjaxResult.success(message);
    }

    /**
     * 新增用户管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户管理
     */
    @RequiresPermissions("system:yh:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YwYh ywYh)
    {
        return toAjax(ywYhService.insertYwYh(ywYh));
    }

    /**
     * 修改用户管理
     */
    @RequiresPermissions("system:yh:edit")
    @GetMapping("/edit/{yhId}")
    public String edit(@PathVariable("yhId") Long yhId, ModelMap mmap)
    {
        YwYh ywYh = ywYhService.selectYwYhByYhId(yhId);
        mmap.put("ywYh", ywYh);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户管理
     */
    @RequiresPermissions("system:yh:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YwYh ywYh)
    {
        return toAjax(ywYhService.updateYwYh(ywYh));
    }

    /**
     * 删除用户管理
     */
    @RequiresPermissions("system:yh:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ywYhService.deleteYwYhByYhIds(ids));
    }
}
